package cn.com.note.utils;

import android.content.Context;
import android.os.Environment;

import com.blankj.utilcode.util.StringUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by sunfulin on 2019/2/11.
 */

public class LoginFileUtils {
    /**
     * root 目录
     */
    public static String rootPath = Environment.getExternalStorageDirectory().getAbsolutePath();

    /**
     * 从sdcard中读取保存的用户名和密码
     *
     * @param filePath
     * @return Map<String,String>
     */
    public static Map<String, String> readFromSDCard(String filePath) {
        Map<String, String> map = new HashMap<>();
        FileReader reader;
        try {
            reader = new FileReader(rootPath + filePath);
            //读取本地文件会有\uFEFF的一个空格现象，需要去掉
            BufferedReader breader = new BufferedReader(reader);
            String line;
            while ((line = breader.readLine().replace("\uFEFF", "")) != null) {
                String strs[] = line.split("\t");
                if (!StringUtils.isEmpty(strs[0])) {
                    map.put(strs[0].trim(), strs[1].trim());
                }
            }
            breader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 从sdcard中读取保存的用户名和密码
     *
     * @param filePath
     * @return Map<String,String>
     */
    public static boolean modifyPassFromSDCard(String filePath,Map<String, String> map) {
        //先清空内容
        clearInfoForFile(rootPath + filePath);
        for (String key: map.keySet()) {
            saveAccountAndPassWordToFile(key,map.get(key),filePath);
        }
        return true;
    }
    /**
     * 清空文件内容
     * @param fileName
     */
    public static void clearInfoForFile(String fileName) {
        File file =new File(fileName);
        try {
            if(!file.exists()) {
                file.createNewFile();
            }
            FileWriter fileWriter =new FileWriter(file);
            fileWriter.write("");
            fileWriter.flush();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 从 assets 中读取保存的用户名和密码
     *
     * @param context
     * @param fileName
     * @return Map<String,String>
     */
    public static Map<String, String> readFromAssets(Context context, String fileName) {
        Map<String, String> map = new HashMap<>();
        try {
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    context.getAssets().open(fileName), "UTF-8"));
            String line;
            while ((line = bf.readLine().replace("\uFEFF", "")) != null) {
                String strs[] = line.split("\t");
                if (!StringUtils.isEmpty(strs[0])) {
                    map.put(strs[0].trim(), strs[1].trim());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }


    /**
     * 第一种方法：
     * 将输入的用户名和密码保存在
     *
     * @param name 输入的用户名
     * @param pass 输入的密码
     */
    public static boolean saveAccountAndPassWordToFile(String name, String pass, String filePath) {
        FileWriter writer;
        try {
            writer = new FileWriter(rootPath + filePath, true);
            writer.append(name + "\t" + pass + "\n");
            writer.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 第一种方法：
     * 将输入的用户名和密码保存在
     *
     * @param name 输入的用户名
     * @param pass 输入的密码
     */
    public static boolean saveAccountAndPassWordToAssets(String name, String pass, String fileName) {
        try {
            String path = "file:///android_asset/" + fileName;
            File sdcardDir = new File(path);
            FileWriter writer;
            writer = new FileWriter(sdcardDir, true);
            writer.append(name + "\t" + pass + "\n");
            writer.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

}
